<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import { debounce } from "@/utils";

const animationDuration = 6000;

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    BarData: {
      type: Object,
      default: function () {
        return {};
      },
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    BarData: {
      deep: true,
      handler(val) {
        this.initChart();
      },
    },
  },
  mounted() {
    // this.initChart();
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize();
      }
    }, 100);
    window.addEventListener("resize", this.__resizeHandler);
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    window.removeEventListener("resize", this.__resizeHandler);
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      let dataset = {
        source: [["product"], ["新增客户数"], ["今日签约客户数"]],
      };
      if (this.BarData.date) {
        dataset.source[0].push(...this.BarData.date);
        dataset.source[1].push(...this.BarData.new_customers);
        dataset.source[2].push(...this.BarData.signing_customers);
      }
      this.chart.setOption({
        legend: {},
        tooltip: {},
        dataset: dataset,
        grid: {
          top: "15%",
          left: "1.2%",
          right: "1%",
          bottom: "3%",
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985",
            },
          },
        },
        xAxis: {
          type: "category",
          axisLine: {
            lineStyle: {
              color: "#9D9D9D", //左边线的颜色
            },
          },
          axisLabel: {
            textStyle: {
              color: "#9D9D9D", //坐标值得具体的颜色
            },
          },
        },
        yAxis: {
          type: "value",
          splitArea: {
            show: true,
            areaStyle: {
              color: ["#fff"],
            },
          },
          axisLine: {
            lineStyle: {
              color: "#9D9D9D", //左边线的颜色
            },
          },
          axisLabel: {
            textStyle: {
              color: "#9D9D9D", //坐标值得具体的颜色
            },
          },
        },
        series: [
          {
            type: "bar",
            seriesLayoutBy: "row",
            itemStyle: {
              //柱形图圆角，鼠标移上去效果
              emphasis: {
                barBorderRadius: [2, 2, 2, 2],
              },

              normal: {
                //柱形图圆角，初始化效果
                barBorderRadius: [2, 2, 2, 2],
              },
            },
            color: "#448EF6", //柱状图 颜色
            barCategoryGap: "50%",
          },
          // {
          //   type: "bar",
          //   seriesLayoutBy: "row",
          //   itemStyle: {
          //     //柱形图圆角，鼠标移上去效果
          //     emphasis: {
          //       barBorderRadius: [2, 2, 2, 2],
          //     },

          //     normal: {
          //       //柱形图圆角，初始化效果
          //       barBorderRadius: [2, 2, 2, 2],
          //     },
          //   },
          //   color: "#8fbbfa", //柱状图 颜色
          // },
          {
            type: "bar",
            seriesLayoutBy: "row",
            itemStyle: {
              //柱形图圆角，鼠标移上去效果
              emphasis: {
                barBorderRadius: [2, 2, 2, 2],
                // itemStyle: {
                //   opacity: 1
                // }
              },
              normal: {
                //柱形图圆角，初始化效果
                barBorderRadius: [2, 2, 2, 2],
              },
            },
            color: "#c3dbfc", //柱状图 颜色
            barCategoryGap: "50%",
          },
        ],
      });
    },
  },
};
</script>
